package com.cx.afc_android;


import android.os.Environment;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cx.afc_android.ahttp.model.HttpData;
import com.cx.afc_android.ahttp.request.MyApi;
import com.cx.afc_android.ahttp.request.UpdateImageApi;
import com.cx.afc_android.ahttp.response.ListBean;
import com.cx.afc_android.ahttp.response.ObjectBean;
import com.cx.afc_android.common.BasicsActivity;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.listener.OnUpdateListener;
import com.hjq.http.model.HttpMethod;
import com.hjq.toast.ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import okhttp3.Call;

public class MainActivity extends BasicsActivity {

    List<ListBean.xBean> list = new ArrayList<>();
    HomeAdapter adapter;

    @BindView(R.id.rv_patient)
    RecyclerView rvPatient;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        rvPatient.setLayoutManager(new LinearLayoutManager(this));
//        adapter = new HomeAdapter(R.layout.home_item, list);
        adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        rvPatient.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        String type = "";
        switch (type){
            case "get":
                EasyHttp.get(this)
                        .api(new MyApi()
                                .setAuthor("xx"))
                        .request(new HttpCallback<HttpData<ObjectBean>>(this) {

                            @Override
                            public void onSucceed(HttpData<ObjectBean> result) {
                                ToastUtils.show("Get 请求成功，请看日志");
                            }
                        });
                break;
            case "post":
                EasyHttp.post(this)
                        .api(new MyApi()
                                .setAuthor("xx"))
                        .request(new HttpCallback<HttpData<ObjectBean>>(this) {

                            @Override
                            public void onSucceed(HttpData<ObjectBean> result) {
                                ToastUtils.show("post 请求成功，请看日志");
                            }
                        });
                break;
            case "update":
                File file = new File(Environment.getExternalStorageDirectory(),  "xx.png");
                EasyHttp.post(this)
                        .api(new UpdateImageApi(file))
                        .request(new OnUpdateListener<Void>() {

                            @Override
                            public void onStart(Call call) {
//                                mProgressBar.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onProgress(int progress) {
//                                mProgressBar.setProgress(progress);
                            }

                            @Override
                            public void onSucceed(Void result) {
                                ToastUtils.show("上传成功");
                            }

                            @Override
                            public void onFail(Exception e) {
                                ToastUtils.show("上传失败");
                            }

                            @Override
                            public void onEnd(Call call) {
//                                mProgressBar.setVisibility(View.GONE);
                            }
                        });
                break;
            case "download":
                EasyHttp.download(this)
                        .method(HttpMethod.GET)
                        .file(new File(Environment.getExternalStorageDirectory(), "微信.apk"))
                        //.url("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
                        .url("http://dldir1.qq.com/weixin/android/weixin708android1540.apk")
                        .md5("2E8BDD7686474A7BC4A51ADC3667CABF")
                        .listener(new OnDownloadListener() {

                            @Override
                            public void onStart(File file) {
//                                mProgressBar.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onProgress(File file, int progress) {
//                                mProgressBar.setProgress(progress);
                            }

                            @Override
                            public void onComplete(File file) {
                                ToastUtils.show("下载完成：" + file.getPath());
//                                installApk(MainActivity.this, file);
                            }

                            @Override
                            public void onError(File file, Exception e) {
                                ToastUtils.show("下载出错：" + e.getMessage());
                            }

                            @Override
                            public void onEnd(File file) {
//                                mProgressBar.setVisibility(View.GONE);
                            }

                        }).start();
                break;

        }

    }

    class HomeAdapter extends BaseQuickAdapter<ListBean.xBean, BaseViewHolder> {

        public HomeAdapter(int layoutResId, List data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final ListBean.xBean item) {
            helper.setText(R.id.name,item.getXx());

        }
    }
}
